﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Win32;

namespace OmBleTool
{
    class Win32Helper
    {
        [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
        private static extern int SetCursorPos(int x, int y);

        [DllImport("user32")]
        private extern static void mouse_event(int dwFlag, int dx, int dy, int dwData, IntPtr deExtraInfo);

        [DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)]
        public static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

        [DllImport("user32.dll", EntryPoint = "EnumDesktopWindows", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDesktopWindowsDelegate lpEnumCallbackFunction, IntPtr lParam);

        public delegate bool EnumDesktopWindowsDelegate(IntPtr hWnd, uint lParam);
        private static readonly object MouseLock = new object();
        public const int KEYEVENTF_KEYDOWN = 0;
        public const int KEYEVENTF_KEYUP = 2;
        public static bool GetAPP(Panel panel1)
        {
            string formTitle = "OmBleConfigurator-Rev2.0.1";
            Process[] myproc = Process.GetProcesses();
            foreach (Process item in myproc)
            {
                if (item.ProcessName.Contains("OmBleConfigurator"))
                {
                    item.Kill();
                    Thread.Sleep(100);
                }
            }
            Process process1 = new Process();
            process1.StartInfo.WorkingDirectory = Environment.CurrentDirectory + "\\";
            process1.StartInfo.FileName = "OmBleConfigurator-Rev2.0.1.exe";
            process1.Start();
            Thread.Sleep(1000);
            bool waitOpen = true;
            while (waitOpen)
            {
                waitOpen = false;
                EnumDesktopWindows(IntPtr.Zero,
                 new EnumDesktopWindowsDelegate(delegate (IntPtr hWnd, uint lparam)
                 {
                     StringBuilder sb = new StringBuilder(256);
                     User.GetWindowText(hWnd, sb, sb.Capacity);
                     if (sb.ToString().Contains("Serial Port Settings Dialog"))
                     {
                         waitOpen = true;
                     }
                     return true;
                 }),
             IntPtr.Zero);
            }
             Thread.Sleep(500);
            DateTime time = DateTime.Now.AddSeconds(5);
            IntPtr ParentHandle = (IntPtr)User.FindWindow(null, formTitle);
            while (ParentHandle == IntPtr.Zero)
            {
                ParentHandle = (IntPtr)User.FindWindow(null, formTitle);
                if (DateTime.Now > time)
                {
                    return false;
                }
            }
            FormInsert(ParentHandle, panel1);
            return true;
        }

        public static void MouseClick(Point p)
        {
            lock (MouseLock)
            {
                //Point point = Control.MousePosition;
                SetCursorPos(p.X, p.Y);
                mouse_event(2, 0, 0, 0, IntPtr.Zero);
                //Delay(50);
                mouse_event(4, 0, 0, 0, IntPtr.Zero);
                //Delay(50);
                //SetCursorPos(point.X, point.Y);
            }
        }
        public static void KeyDown(Keys key)
        {
            keybd_event(key, 0, KEYEVENTF_KEYDOWN, 0);
            //Delay(50);
            keybd_event(key, 0, KEYEVENTF_KEYUP, 0);
        }
        public static void KeyDown(Keys key1, Keys key2)
        {
            keybd_event(key1, 0, KEYEVENTF_KEYDOWN, 0);
            //Delay(50);
            keybd_event(key2, 0, KEYEVENTF_KEYDOWN, 0);
            //Delay(50);
            keybd_event(key1, 0, KEYEVENTF_KEYUP, 0);
            //Delay(50);
            keybd_event(key2, 0, KEYEVENTF_KEYUP, 0);
        }
        public static void FormInsert(IntPtr hwnd, Panel panel)
        {
            User.SetParent(hwnd, panel.Handle);
            User.SetWindowLong(hwnd, User.GWL_STYLE, User.WS_VISIBLE);
            User.MoveWindow(hwnd, 0, 0, panel.Width + 17, panel.Height + 40, 1);
            Thread.Sleep(500);
            User.ShowWindow(hwnd, User.SW_SHOWDEFAULT);
        }
    }
}
